package com.bym.service.communication.hystrix;

import com.bym.service.communication.AuthorityFeignClient;
import com.bym.vo.JwtToken;
import com.bym.vo.UsernameAndPassword;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <h1>OpenFeign 集成 Hystrix 的另一种模式</h1>
 */
@Slf4j
@Component
public class AuthorityFeignClientFallbackFactory implements FallbackFactory<AuthorityFeignClient> {

    @Override
    public AuthorityFeignClient create(Throwable throwable) {
        log.info("authority feign client get token by feign request error (Hystrix FallbackFactory): [{}]",
                throwable.getMessage(), throwable);
        return new AuthorityFeignClient() {

            @Override
            public JwtToken getTokenByFeign(UsernameAndPassword usernameAndPassword) {
                return new JwtToken("bym");
            }
        };
    }
}
